<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>business_add</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <style>
        .view-form .layui-input-block, .view-form .layui-input-inline {
            line-height: 36px;
            background: #eeeeee;
            padding-left: 20px;
            font-size: 16px;
        }
    </style>
</head>
<body>
<div style="padding: 20px;">
    <form class="layui-form " action="">
        <input id="id" name="ID" type="hidden"/>
        <div class="layui-form-item name">
            <label class="layui-form-label">物业名称：</label>
            <div class="layui-input-block">
                <input name="CODE" lay-verify="required" placeholder="" autocomplete="off"
                       class="layui-input" style="width: 500px;">
            </div>
        </div>
        <!--  <div class="layui-form-item type">
              <label class="layui-form-label" style=" width: 65px;">收费类型</label>
              <div class="layui-input-block">
                  <input type="checkbox" name="gdsr" id="one" lay-skin="primary" title="固定收费" value="1," >
                  <input type="checkbox" name="cbsf" id="two" lay-skin="primary" title="抄表收费" value="2,">
                  <input type="checkbox" name="mjsf" id="three" lay-skin="primary" title="按面积收费" value="3,">
              </div>
          </div>-->

        <!-- <div class="layui-form-item datas">
             <label class="layui-form-label" style=" width: 65px;">收费日期</label>
             <div class="layui-input-block">
                 <input type="checkbox" name="tqrsf" id="free" lay-skin="primary" title="提前日收费" value="1," >
                 <input type="checkbox" name="gdrsf" id="five" lay-skin="primary" title="固定日收费" value="2,">
                 <input type="checkbox" name="cbhsf" id="six" lay-skin="primary" title="抄表后收费"  value="3,">
             </div> <!--<button class="layui-btn layui-btn-small" style=" height: 37px;"><i class="layui-icon"></i></button>-->
        <!-- </div>-->
        <div class="layui-form item">
            <label class="layui-form-label">费用单价：</label>
            <div class="layui-input-block" id="xzdjs" style="  margin-bottom: 16px;">
                <a class="layui-btn xzdj">新增单价</a>
            </div>
        </div>

        <!--

                <div class="contact-person-box "style=" margin-bottom: 10px;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style=" width: 65px;">单价单价</label>
                        <div class="layui-input-inline">
                            <input type="text" name="PRICE_NAME" lay-verify="required" autocomplete="off" class="layui-input" style=" padding-left: 10px; margin-left: 15px;">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 30px;padding-right: 10px;padding-left: 10px;margin-left: 12px;">单价</label>
                        <div class="layui-input-inline">
                            <input type="tel" name="PRICE_TYPE" lay-verify="required" autocomplete="off" class="layui-input" value="3333">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline layui-input-extra-content" style="width: 100px; height: 38px;">
                            &lt;!&ndash;<button class="layui-btn layui-btn-small" style=" height: 37px;"><i class="layui-icon"></i></button>&ndash;&gt;
                            <a class="layui-btn dele" click="del(this)">删除</a>
                        </div>
                    </div>
                </div>

        -->

        <!--<div class="contact-person-box ">
              <div class="layui-inline">
              <input id="priceid" name="PRICE_ID" type="hidden"/>
                  <label class="layui-form-label">单价名称：</label>
                  <div class="layui-input-inline" style="margin-top:11px;">
                      <input type="text" name="PRICE_NAME" lay-verify="required" autocomplete="off" class="layui-input" style=" padding-left: 10px; margin-left: 15px;">
                  </div>
              </div>
              <div class="layui-inline">
                  <label class="layui-form-label" style="width: 30px;padding-right: 10px;padding-left: 10px;margin-left: 12px;">单价</label>
                  <div class="layui-input-inline" style="margin-top:11px;">
                      <input type="tel" name="PRICE_TYPE" lay-verify="required" autocomplete="off" class="layui-input">
                  </div>
              </div>
              <div class="layui-inline">
                  <div class="layui-input-inline layui-input-extra-content" style="width: 100px; height: 38px;">
                                          <select name="PRICE_CURRENCY" lay-filter="unit">
                                                 <option value="0">元/吨</option>
                                                  <option value="1">元/㎡·天</option>
                                                  <option value="2">元/日</option>
                                                  <option value="3">元/月</option>
                                                  <option value="4">元/年</option>
                                                  <option value="5">元</option>
                                             </select>
                                      </div>
                              </div>
              <div class="layui-inline">
                  <div class="layui-input-inline layui-input-extra-content" style="width: 100px; height: 38px;">
                      <a class="layui-btn dele">删除</a>
                  </div>
              </div>
                    </div>-->

        <div class="layui-form-item add"></div>
        <div class="layui-form-item open">
            <label class="layui-form-label">是否启用：</label>
            <div class="layui-input-block">
                <input type="checkbox" checked="" id="sfqy" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest"
                       lay-text="启用|关闭">
            </div>
        </div>
        <div class="layui-form-item layui-form text">
            <label class="layui-form-label">备注信息：</label>
            <div class="layui-input-block">
                <textarea name="INFO" placeholder="请输入备注信息" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item operate-button-group">
            <div class="layui-input-block" id="bccz">
                <button class="layui-btn" lay-submit="" lay-filter="room">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="../../../static/js/common/queryUtils.js"></script>
<script type="text/javascript" src="../../../static/layer/layui.js"></script>


<script>
    var operateType;
    var id;
    var $;
    layui.use(['jquery', 'element', 'layer', 'laydate', 'form', 'upload'], function () {
        $ = layui.jquery, element = layui.element, layer = layui.layer, laydate = layui.laydate, form = layui.form;
        var upload = layui.upload;
        /*var $='';*/
        //操作类型：add新增，edit修改，view查看
        operateType = QueryUtils.GetQueryString("type");
        id = QueryUtils.GetQueryString("id");
        $("#id").val(id);

        $(".xzdj").click(function (obj) {
            /* var str = '<div class="contact-person-box xxx"style=" margin-bottom: 10px;">\n' +*/
            var str = '<div class="contact-person-box"style=" margin-bottom: 10px;">\n' +
                '                                  <div class="layui-inline">\n' +
                '                                                <label class="layui-form-label">单价名称：</label>\n' +
                '                                               <div class="layui-input-inline">\n' +
                '                                                   <input type="text" name="PRICE_NAME" lay-verify="required" autocomplete="off" class="layui-input" style=" padding-left: 10px; margin-left: 15px;">\n' +
                '                                                      </div>\n' +
                '                                        </div>\n' +
                '                                    <div class="layui-inline">\n' +
                '                                                <label class="layui-form-label" style="width: 30px;padding-right: 10px;padding-left: 10px;margin-left: 12px;">单价</label>\n' +
                '                                                <div class="layui-input-inline">\n' +
                '                                                            <input type="tel" name="PRICE_TYPE" lay-verify="required" autocomplete="off" class="layui-input">\n' +
                '                                                        </div>\n' +
                '                                            </div>\n' +
                '                        <div class="layui-inline">\n' +
                '                                <div class="layui-input-inline layui-input-extra-content" style="width: 100px; height: 38px;">\n' +
                '                                        <!--<button class="layui-btn layui-btn-small" style=" height: 37px;"><i class="layui-icon"></i></button>-->\n' +
                '                                        <a class="layui-btn dele" name="del" onclick="operate.remove(this);">删除</a>\n' +
                '                                    </div>\n' +
                '                            </div>\n' +
                '                              </div>';
            $(".add").append(str);
            form.render("select");
        });

        switch (operateType) {
            case "add":
                break;
            case "edit":
                // $(".open").remove();
                $.ajax({
                    url: "/property/propertyinfo",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {

                            if (res.data.ID == "a14e6f2e190e4492badac72e9b42152e" || res.data.ID == "ddcf9b35bbe04cd88834e21c1fd74f32" || res.data.ID == "669791dac4894160bc8d79b484519e96" || res.data.ID == "7b05982a081e41deb9b46a1f6f5ed5fa") {
                                $(".open").remove();
                            }

                            $("input[name='CODE']").val(res.data.CODE);
                            /* var a =  res.data.CHARGE_TYPE;
                             var b = a.split(',');*/
                            /* for(var i =0 ;i<b.length;i++){
                                 if(b[i]=='1'){
                                     $("#one").attr("checked","checked");
                                 }else if(b[i]=='2'){
                                     $("#two").attr("checked","checked");
                                 }else if(b[i]=='3'){
                                     $("#three").attr("checked","checked");
                                 }
                             }*/
                            /* var c = res.data.CHARGE_DATE;
                             var d = c.split(",");
                             for(var i=0;i<d.length;i++){
                                 if(d[i]=='1'){
                                     $("#free").attr("checked","checked");
                                 } else if(d[i]=='2'){
                                     $("#five").attr("checked","checked");
                                 }else if(d[i]=='3'){
                                     $("#six").attr("checked","checked");
                                 }
                             }*/
                            /* form.render("checkbox");*/
                            if (res.data.IS_OPEN === "on") {
                                $("input[name='IS_OPEN']").parent().html('<input type="checkbox" checked="" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">');
                            } else if (res.data.IS_OPEN === "off") {
                                $("input[name='IS_OPEN']").parent().html('<input type="checkbox" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">');
                            }
                            form.render();
                            $("textarea[name='INFO']").val(res.data.INFO);
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                $.ajax({
                    url: "/property/pricelist",
                    data: {PROPERTY_SET_ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            for (var i = 0; i < res.data.length; i++) {
                                var str = '<div class="contact-person-box ">\n' +
                                    '            <div class="layui-inline">\n' +
                                    '            <input id="priceid" name="PRICE_ID" type="hidden" value=\"' + res.data[i].ID + '\"/>\n' +
                                    '                <label class="layui-form-label">单价名称：</label>\n' +
                                    '                <div class="layui-input-inline">\n' +
                                    '                    <input type="text" name="PRICE_NAME" value=\"' + res.data[i].PRICE_NAME + '\" lay-verify="required" autocomplete="off" class="layui-input" style=" padding-left: 10px; margin-left: 15px;">\n' +
                                    '                </div>\n' +
                                    '            </div>\n' +
                                    '            <div class="layui-inline">\n' +
                                    '                <label class="layui-form-label" style="width: 30px;padding-right: 10px;padding-left: 10px;margin-left: 12px;">单价</label>\n' +
                                    '                <div class="layui-input-inline">\n' +
                                    '                    <input type="tel" name="PRICE_TYPE" value=\"' + res.data[i].PRICE_TYPE + '\" lay-verify="required" autocomplete="off" class="layui-input" >\n' +
                                    '                </div>\n' +
                                    '            </div>\n' +
                                    '            <div class="layui-inline">\n' +
                                    '                <div class="layui-input-inline layui-input-extra-content" style="width: 100px; height: 38px;">\n' +
                                    '                    <a class="layui-btn dele" onclick="operate.removes(this);">删除</a>\n' +
                                    '                </div>\n' +
                                    '            </div>\n' +
                                    '                  </div>';
                                $(".add").append(str);
                                /* $("select[name='PRICE_CURRENCY']").val(res.data[i].PRICE_CURRENCY);*/
                            }
                            form.render("select");
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
            case "view":
                $.ajax({
                    url: "/property/propertyinfo",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {

                            $("input[name='CODE']").val(res.data.CODE);
                            $("input[name='CODE']").attr("disabled", "disabled");
                            /* var a =  res.data.CHARGE_TYPE;
                             var b = a.split(',');
                             for(var i =0 ;i<b.length;i++){
                                 if(b[i]=='1'){
                                     $("#one").attr("checked","checked");
                                 }else if(b[i]=='2'){
                                     $("#two").attr("checked","checked");
                                 }else if(b[i]=='3'){
                                     $("#three").attr("checked","checked");
                                 }
                                 $("input[type='checkbox']").attr("disabled","false");
                             }
                             var c = res.data.CHARGE_DATE;
                             var d = c.split(",");
                             for(var i=0;i<d.length;i++){
                                 if(d[i]=='1'){
                                     $("#free").attr("checked","checked");

                                 } else if(d[i]=='2'){
                                     $("#five").attr("checked","checked");

                                 }else if(d[i]=='3'){
                                     $("#six").attr("checked","checked");

                                 }
                                 $("a").attr("disabled","false");
                             }
                             form.render("checkbox");*/
                            $("#xzdjs").html("<a class=\"layui-btn \">新增单价</a>");
                            $(".xzdj").attr("disabled", "false");
                            if (res.data.IS_OPEN === "on") {
                                $("input[name='IS_OPEN']").parent().html('<input type="checkbox" checked="" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">');
                            } else if (res.data.IS_OPEN === "off") {
                                $("input[name='IS_OPEN']").parent().html('<input type="checkbox" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">');
                            }
                            $("textarea[name='INFO']").val(res.data.INFO);
                            $("input[name='IS_OPEN']").attr("disabled", "disabled");
                            $("button").attr("disabled", "false");
                            $("textarea").attr("readonly", "readonly")
                            form.render("checkbox");
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                $.ajax({
                    url: "/property/pricelist",
                    data: {PROPERTY_SET_ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            for (var i = 0; i < res.data.length; i++) {
                                var str = '<div class="contact-person-box ">\n' +
                                    '            <div class="layui-inline">\n' +
                                    '            <input id="priceid" name="PRICE_ID" type="hidden" value=\"' + res.data[i].ID + '\"/>\n' +
                                    '                <label class="layui-form-label">单价名称：</label>\n' +
                                    '                <div class="layui-input-inline">\n' +
                                    '                    <input type="text" name="PRICE_NAME" value=\"' + res.data[i].PRICE_NAME + '\" lay-verify="required" autocomplete="off" class="layui-input" style=" padding-left: 10px; margin-left: 15px;" disabled>\n' +
                                    '                </div>\n' +
                                    '            </div>\n' +
                                    '            <div class="layui-inline">\n' +
                                    '                <label class="layui-form-label" style="width: 30px;padding-right: 10px;padding-left: 10px;margin-left: 12px;">单价</label>\n' +
                                    '                <div class="layui-input-inline">\n' +
                                    '                    <input type="tel" name="PRICE_TYPE" value=\"' + res.data[i].PRICE_TYPE + '\" lay-verify="required" autocomplete="off" class="layui-input" disabled>\n' +
                                    '                </div>\n' +
                                    '            </div>\n' +
                                    '            <div class="layui-inline">\n' +
                                    '                <div class="layui-input-inline layui-input-extra-content" style="width: 100px; height: 38px;">\n' +
                                    '                    <a class="layui-btn dele" onclick="operate.remove(this);">删除</a>\n' +
                                    '                </div>\n' +
                                    '            </div>\n' +
                                    '                  </div>';
                                $(".add").append(str);
                                /* form.render("select");
                                 $("select[name='PRICE_CURRENCY']").val(res.data[i].PRICE_CURRENCY);*/
                            }
                            form.render("select");
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
        }
        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            if (this.checked === true) {
                layer.tips('温馨提示：保存成功后项目立即启用' + '', data.othis);
            } else if (this.checked === false) {
                layer.tips('温馨提示：保存成功后项目需开启后可用' + '', data.othis);
            }
        });
        /* //监听单元select
         form.on('select(unitSelect)', function (data) {
             //initFloor(data.value);
         });*/
        //监听提交
        form.on('submit(room)', function (data) {
            if (!data.field.IS_OPEN) {
                data.field.IS_OPEN = "off"; //如果是否开启为未开启则设置为off
            }
            var data1 = data.field;
            var prices = new Array();
            $(".contact-person-box").each(function () {
                var price = {};
                price.PRICE_ID = $(this).find("input[name='PRICE_ID']").val();
                price.PRICE_NAME = $(this).find("input[name='PRICE_NAME']").val();
                price.PRICE_TYPE = $(this).find("input[name='PRICE_TYPE']").val();
                prices.push(price);
            });
            data = {};
            data.PROPERTYS = prices;
            var DATAS = {};
            DATAS.DATA = JSON.stringify(data);
            if (operateType === "add") {
                layer.msg('确认保存新增物业吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/property/savePropertyAndPrice",
                            data: {DATAS: DATAS, data1: data1},
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭新建房间页面
                                    parent.reload()// 父页面表格刷新
                                    parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                } else if (res.errcode === "1") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            } else if (operateType === "edit") {
                layer.msg('确认编辑新增物业吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/property/editPropertyAndPrice",
                            data: {DATAS: DATAS, data1: data1},
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭修改房间页面
                                    parent.reload();    // 父页面表格刷新
                                    parent.layer.msg("修改成功", {offset: '150px'});   // 父页面弹出修改成功
                                } else if (res.errcode === "1") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            }
            return false;
        });
    });
    var operate = {
        remove: function (obj) {
            $(obj).parent().parent().parent().remove();
        },
        removes: function (obj) {

            var ID = $(obj).parent().parent().parent().find("input[name='PRICE_ID']").val();

            layer.msg('确认删除原有的价格吗', {
                time: 0
                , btn: ['确定', '取消']
                , shade: 0.3
                , yes: function (index) {
                    layer.close(index); // 关闭当前提示框
                    $.ajax({
                        url: "/property/delPrice",
                        data: {ID: ID, IS_DEL: 1},
                        dataType: "json",
                        type: "post",
                        async: false,
                        success: function (res) {
                            if (res.errcode === "0") {
                                $(obj).parent().parent().parent().remove();//删除显示原有的数据
                                /*  var i = parent.layer.getFrameIndex(window.name);
                                  parent.layer.close(i);  // 关闭新建房间页面
                                  parent.reload()// 父页面表格刷新
                                  parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功*/
                            } else if (res.errcode === "1") {
                                layer.msg(res.errmsg, function () {
                                });
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.error(XMLHttpRequest.status);
                            console.error(XMLHttpRequest.readyState);
                            console.error(textStatus);
                        }
                    });
                }
            });

        }
    }
</script>
</body>
</html>